<?php

namespace App\Http\Controllers\Admin\Superadmin;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LinkController extends Controller
{	
	/*
		关于友情链接添加的方法
	*/
	public function getAdd()
	{
		return view('admin.superadmin.link.add');
	}
 
	/*
		执行数据的插入
	*/

	public function postInsert(request $request)
	{	
		
		//数据验证

		 $this->validate($request, [
        	'fr_name' => 'required',
        	'fr_link' => 'required',     
    	],[
    		'fr_name.required'=>'网站名称不能为空',
    		'fr_link.required'=>'网站链接不能为空',
    		

    	]);
			//提取数据
		$data = $request->only(['fr_name','fr_link']);
	
	

		$res = DB::table('hs_friend')->insert($data);


			//判断添加是否成功
		if($res){
			return redirect('/admin/superadmin/link/add')->with('success','添加成功');
		}else{
			return back()->with('error','用户添加失败');	 
		}
	}	


	/*
		友情链接列表页
	*/
	
	public function getIndex(request $request){
		
		//获取分页大小
		$num = $request->input('num',5);
		if($request->input('keywords')){
			$res = DB::table('hs_friend')->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
		}else{
			$res = DB::table('hs_friend')->paginate($num);
		}

		//提取请求数据
		$data = $request->all();

		
		return view('admin.superadmin.link.index',['res'=>$res,'request'=>$data]);
		
	}	


	/*
		友情链接删除的操作
	*/

	public function getDelete($id)	
	{
			//执行删除
		$res = DB::table('hs_friend')->where('id',$id)->delete();
	if($res){
		return redirect('/admin/superadmin/link/index')->with('success','删除成功');
	}else{
		return back()->with('error',删除失败);
	}

	}


	/*
		友情链接修改的操作		
	*/

	public function getEdit($id)
	{
		$user = DB::table('hs_friend')->where('id',$id)->first();
	
		return view('admin.superadmin.link.edit',['user'=>$user]);
	}


	/*
		执行数据的修改	
	*/

	public function postUpdate(request $request)
	{

		

    	//获取id
    	$id = $request->only(['id']);
    	// $data = $request->except('_token');
    	// dd($data);
    	//详细数据
		$data = $request->only(['fr_name','fr_link']);
		//执行修改
		$res = DB::table('hs_friend')->where('id',$id)->update($data);
		//判断是否添加成功
		if($res){
			return redirect('/admin/superadmin/link/index')->with('success','修改成功');
		}else{
			return back()->with('error','修改失败');
		}		
	}	





}	
